$(document).ready(function(){
    setTimeout("closeResultMessages()",5000);
    if(typeof(bkLib)!='undefined'){
        bkLib.onDomLoaded(function(){
            if(typeof(document.getElementById("description"))!='undefined'){
                var descriptionEditor = new nicEditor({
                    fullPanel : true,
                    maxHeight:500
                }).panelInstance('description');
                descriptionEditor.addEvent("blur", function() {
                    document.getElementById("description").value = descriptionEditor.instanceById("description").getContent();
                    $("#tagResult").height($("#toGetHeight").height()+150);
                });
            }
            if(typeof(document.getElementById("rbHtml"))!='undefined'){
                var addEditor = new nicEditor({
                    fullPanel : true,
                    maxHeight:500
                }).panelInstance('rbHtml');
                addEditor.addEvent("blur", function() {
                    document.getElementById("rbHtml").value = addEditor.instanceById("rbHtml").getContent();
                });
            }
        });
    }
    $('input:radio[name=view_type]').change(function (){
        if($('input:radio[name=view_type]:checked').val() == 'html') {
            $('#divRelateBoxArticle').hide();
            $('#divRelateBoxHtml').show();
        }else if($('input:radio[name=view_type]:checked').val() == 'article'){
            $('#divRelateBoxHtml').hide();
            $('#divRelateBoxArticle').show();
        }
    })

    if($('input:radio[name=view_type]:checked').val() == 'html') {
        $('#divRelateBoxArticle').hide();
        $('#divRelateBoxHtml').show();
    }else if($('input:radio[name=view_type]:checked').val() == 'article'){
        $('#divRelateBoxHtml').hide();
        $('#divRelateBoxArticle').show();
    }

    $('#rbSearchArticleSubmit').click(function(){
        var keyword = $('#rbKeyword').val();
        var dateFrom = $('#dateFrom').val();
        var dateTo = $('#dateTo').val();
        var cat_id = $('#rbCategoryId').val();

        var postData = "cat_id="+cat_id;
        postData = postData + "&keyword="+keyword;
        postData = postData + "&dateFrom="+dateFrom;
        postData = postData + "&dateTo="+dateTo;
        postData = postData + "&cat_id="+cat_id;
        postData = postData + "&categoryActionType=searchArticleRelateBox";
        $.ajax({
            url: '/'+ADMIN_SECTION+'/category',
            type: 'POST',
            dataType: 'json',
            data: postData,
            timeout: 1000,
            error: function(){
                alert('Network connection error!');
            },
            success: function(data){
                $("#rbSearchArticleResult").html(data.html);

            }
        });


    });


    $('#rbSelectArticle').click(function(){
        listbox_moveOptions('rbSearchArticleResult','rbSearchArticleSelect');
    });
    $('#rbRemoveArticle').click(function(){
        listbox_removeOptions('rbSearchArticleSelect');
    });

    $('#addCategorySubmit').click(function(){
        if($('#categoryname').val()==''){
            alert("Category name can not be empty!");
            $('#categoryname').focus();
            return false;
        }

        var checkViewType = $('input:radio[name=view_type]:checked').val();
        if(checkViewType == "html") {
            if($("#rbHtml").val()!='<br>'){
                if($('#box_title').val()==''){
                    alert("Box title can not be empty!");
                    $('#box_title').focus();
                    return false;
                }
            }
        }else if(checkViewType == "article"){
            var src = document.getElementById('rbSearchArticleSelect');
            var totalOptions = src.options.length;
            if(totalOptions>0){
                if($('#box_title').val()==''){
                    alert("Box title can not be empty!");
                    $('#box_title').focus();
                    return false;
                }

            }
        }
        listbox_selectall('rbSearchArticleSelect',true);
        document.formAddCategory.submit();
    });

});


function viewAddArticle(){
    window.location = "/"+ADMIN_SECTION+"/article?articleActionType=viewAddArticle";
}
function editArticle(id){
    window.location = "/"+ADMIN_SECTION+"/article?id="+id+"&articleActionType=editArticle";
}

function deleteArticle(id,txtAlert){
    if (confirm("Do you want to delete article "+txtAlert+"?")){
        $.ajax({
            url: '/'+ADMIN_SECTION+'/article',
            type: 'POST',
            data: "id="+id+"&articleActionType=deleteArticle",
            error: function(){
                alert('Error loading article infomation');
            },
            success: function(){
                window.location.reload(true);
            }
        });
    }
}

function closeResultMessages(){
    if ( $("#rsMessages").length > 0 ) {
        document.getElementById('rsMessages').style.display = 'none';
    }
}

function closeAlertMessages(id){
    document.getElementById(id).style.display = 'none';
}

function submitFormAddEditArticle(type){
    
    listbox_selectall('rbSearchArticleSelect',true);
    
    if($("#title").val()==''){
        $("#titleAlert").show();
        setTimeout("closeAlertMessages('titleAlert')",8000);
        $("#title").focus();
        return false;
    }

    if($("#categoryId").val()==''){
        $("#categoryAlert").show();
        setTimeout("closeAlertMessages('categoryAlert')",8000);
        $("#categoryId").focus();
        return false;
    }

    if($("#short_description").val()==''){
        $("#shortDescriptionAlert").show();
        setTimeout("closeAlertMessages('shortDescriptionAlert')",80000);
        $("#short_description").focus();
        return false;
    }
    if($("#description").val()=='' || $("#description").val()=='<br>'){
        $("#descriptionAlert").show();
        setTimeout("closeAlertMessages('descriptionAlert')",80000);
        $("#description").focus();
        return false;
    }
    
    if(type=='add'){
        document.formAddArticle.submit();
    }
    if(type=='edit'){
        document.formEditArticle.submit();
    }

}

function genTags(){
    if($("#title").val()==''){
        $("#titleAlert").show();
        setTimeout("closeAlertMessages('titleAlert')",8000);
        $("#title").focus();
        return false;
    }
    if($("#short_description").val()==''){
        $("#shortDescriptionAlert").show();
        setTimeout("closeAlertMessages('shortDescriptionAlert')",80000);
        $("#short_description").focus();
        return false;
    }
    if($("#description").val()=='' || $("#description").val()=='<br>'){
        $("#descriptionAlert").show();
        setTimeout("closeAlertMessages('descriptionAlert')",80000);
        $("#description").focus();
        return false;
    }
    //        var frequency = new Array();
    //        frequency.push($("#frequency1").val(),$("#frequency2").val(),$("#frequency3").val(),$("#frequency4").val(),$("#frequency5").val(),$("#frequency6").val());
    $.ajax({
        url: '/'+ADMIN_SECTION+'/gen-tag',
        type: 'POST',
        data: "title=" + $("#title").val() + "&description=" + $("#short_description").val() + "&detail=" + $("#description").val() + "&frequency=" + $("#frequency1").val()+','+$("#frequency2").val()+','+$("#frequency3").val()+','+$("#frequency4").val()+','+$("#frequency5").val()+','+$("#frequency6").val(),
        error: function(){
            alert('Error loading tags infomation!');
        },
        success: function(data){
            $("#keywordAlert").html('<span class="success">Generate tags successful!</span>');
            $("#keywordAlert").show('slow');
            setTimeout("hideKeyWordAlert()",5000);

            $("#tagResult").html(data);
        }
    });


}
function clearTags(){
    $("input[name=keyword[]]").val('');
}
function addPhotos(place){
    windowphoto = window.open("/"+ADMIN_SECTION+"/photos?place="+place, "Images manger popup", "width=800,height=600,scrollbars=yes");
}

checked=false;
function checkedAll (formId) {
    var aa= document.getElementById(formId);
    if (checked == false)        {
        checked = true;
    }else{
        checked = false;
    }
    for (var i =0; i < aa.elements.length; i++){
        aa.elements[i].checked = checked;
    }
}

function actionMultil(action){
    if(action=='delete'){
        if (confirm("Do you want to delete these articles?")){
            document.getElementById('formView').submit();
        }else{
            document.getElementById('action').value = '';
        }
    }
    if(action=='yes'){
        if (confirm("Do you want to set these articles's status to yes?")){
            document.getElementById('formView').submit();
        }else{
            document.getElementById('action').value = '';
        }
    }
    if(action=='pending'){
        if (confirm("Do you want to set these articles's status to pendding?")){
            document.getElementById('formView').submit();
        }else{
            document.getElementById('action').value = '';
        }
    }
    if(action=='no'){
        if (confirm("Do you want to set these articles's status to no?")){
            document.getElementById('formView').submit();
        }else{
            document.getElementById('action').value = '';
        }
    }
}

function statusFilter(){
    document.getElementById('formStatusView').submit();
}

function keyword_remove(no){
    if(document.getElementById('removedTags').value==''){
        document.getElementById('removedTags').value = document.getElementById('removedTags').value + document.getElementById('key'+no).value;
    }else{
        document.getElementById('removedTags').value = document.getElementById('removedTags').value + ',' + document.getElementById('key'+no).value;
    }
    document.getElementById('k'+no).innerHTML = '';
    document.getElementById('k'+no).style.display = 'none';
    $("#keywordAlert").hide('slow');
    $("#keywordAlert").html('<span class="success">Remove keyword successful!</span>');
    $("#keywordAlert").show('slow');
    setTimeout("hideKeyWordAlert()",5000);
}
function hideKeyWordAlert(){
    $("#keywordAlert").hide('slow');
}
function keyword_spam(no){
    if(no!=''){
        if(document.getElementById('removedTags').value==''){
            document.getElementById('removedTags').value = document.getElementById('removedTags').value + document.getElementById('key'+no).value;
        }else{
            document.getElementById('removedTags').value = document.getElementById('removedTags').value + ',' + document.getElementById('key'+no).value;
        }
        $.ajax({
            url: '/'+ADMIN_SECTION+'/gen-tag',
            type: 'GET',
            data: "keyword=" + document.getElementById('key'+no).value,
            error: function(){
                alert('Error add spam!');
            },
            success: function(){
                $("#keywordAlert").hide('slow');
                document.getElementById('k'+no).innerHTML = '';
                document.getElementById('k'+no).style.display = 'none';
                $("#keywordAlert").html('<span class="success">Add to spam successful!</span>');
                $("#keywordAlert").show('slow');
                setTimeout("hideKeyWordAlert()",5000);
            }
        });
    }
}
function frontPage(id,obj){
    if(id!=''){
        $.ajax({
            url: '/'+ADMIN_SECTION+'/article',
            type: 'POST',
            data: "id="+id+"&articleActionType=updateFrontPageDate",
            error: function(){
                alert('Error loading article infomation');
            },
            success: function(data){
                if(data=='yes'){
                    obj.src = ADMIN_IMAGES_URL+'/icons/circle-green.png';
                    obj.title = 'Hot news actived!';
                }else if(data=='no'){
                    obj.src = ADMIN_IMAGES_URL+'/icons/circle-red.png';
                    obj.title = 'Not show in Hot news!';
                }else{
                    window.location.reload(true);
                }
            }
        });
    }
}

function removeAttachFile(attachId){
    valueRemoveAttach = '<input type="hidden" name="removeAttachs[]" value="'+ attachId +'"/>';
    divHtml = valueRemoveAttach + $("#hiddenAttachDiv").html();
    $("#hiddenAttachDiv").html(divHtml);
    $("#removeAttachDiv_"+attachId).remove();
}